package com.totoro.transfer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class T20180404 {

    public static void transfer() {
        //String dir ="/Users/swy/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/bd63c81b829ccfbd21d0b7bc3439ae70/Message/MessageTemp/3ae126ce7089bf77172056a246750e3a/File/data.csv";
        //String dir = "/Users/swy/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/bd63c81b829ccfbd21d0b7bc3439ae70/Message/MessageTemp/b57159745b91a3500b5571651c6ad49f/File";
        //String dir = "/Users/swy/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/bd63c81b829ccfbd21d0b7bc3439ae70/Message/MessageTemp/f15e8c4552cc942be54d7c03940014b1/File";
        String dir = "/Users/swy/Documents/transfer";
        //图片
        Map<String, String> cache = new HashMap();
        try {
            BufferedReader readerCvs = new BufferedReader(new FileReader(new File(dir + "/data.csv")));
            String line1 = null;
            while ((line1 = readerCvs.readLine()) != null) {
                //String key = line1.replaceAll("^.+/([^/]+)$", "$1");
                cache.put(line1.split(",")[0], line1.split(",")[1]);
                //cache.put(line1.split(",")[0], line1.split(",")[1]);
            }
            readerCvs.close();


            File dirFile = new File("/Users/swy/Documents/transfer/asr_result/JSON1");
            File[] fileList = dirFile.listFiles();
            System.out.println(String.format("开始处理文件..."));

            BufferedWriter writer = new BufferedWriter(new FileWriter(new File("/Users/swy/Documents/transfer/" + "/sql4309_1.sql")));

            for (int i = 0; i < fileList.length; i++) {
                if(!fileList[i].getName().contains(".json")){
                    continue;
                }
                //region json处理
                //读取文件
                StringBuffer sb = new StringBuffer();
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(fileList[i]));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }
                    reader.close();
                    System.out.println("filename" + fileList[i].getName());
                    System.out.println("filename" + sb.toString());
                    System.out.println("filecontent" + sb.toString());
                } catch (Exception ex) {

                }
                JSONArray parameter = JSONArray.parseArray(sb.toString());
                //写入json
                JSONObject result = new JSONObject();
                result.put("name", fileList[i].getName().replace(".json", ""));
                result.put("seconds", parameter.getJSONObject(parameter.size() - 1).getIntValue("ed") / 1000.00f);
                JSONArray rects = new JSONArray();

                for (int k = 0; k < parameter.size(); k++) {
                    JSONObject temp = new JSONObject();
                    temp.put("start_seconds", parameter.getJSONObject(k).getIntValue("bg") / 1000.00f);
                    temp.put("end_seconds", parameter.getJSONObject(k).getIntValue("ed") / 1000.00f);
                    temp.put("x1", (parameter.getJSONObject(k).getIntValue("bg") / 1000.00f) * 70 + 20.5);
                    temp.put("x2", (parameter.getJSONObject(k).getIntValue("ed") / 1000.00f) * 70 + 20.5);

                    temp.put("y1", 0);
                    temp.put("y2", 200);
                    temp.put("id", System.currentTimeMillis() + k);
                    temp.put("noise", "");
                    temp.put("sex", "");
                    temp.put("identity", "");

                    temp.put("text", parameter.getJSONObject(k).getString("onebest"));
                    rects.add(temp);
                }
                result.put("rects", rects);
                System.out.println(result.toJSONString());
                //region SQL生成
                String sql = String.format(
                        "%s VALUES (414,'201804041828','{\"image\":\"%s\"}',1,'{}',0,0,1,0,0,0,'{\"markData\":%s}',null);\n",
                        "INSERT INTO `tags_task_libs` (`tid`, `batch_id`, `question`, `type`, `properties`, `ctime`, `uid`, `status`, `lock`, `lock_status`, `lock_time`, `answer`, `log_id`)",
                        cache.get(fileList[i].getName().replace(".json", "")),
                        result.toJSONString()
                );
                //System.out.println(sql);
                writer.write(sql);

                //endregion
            }
            writer.flush();
            writer.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String[] args) {
        transfer();
    }
}
